﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="/js/header.js"></script>
    <title>验证动态口令</title>
</head>
<body style="background-color: white;">
    <section class="container-fluid" style="background-color: white;">
        <form class="form-horizontal m15_t" id="form" onsubmit="return false;">
            <div class="tab-content">
                <div class="tab-pane fade in active">

                    <div class="form-group" id="showCodeDiv" style="display:none;">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>请扫描二维码：</label>
                        <div class="wd250 m15_l">
                            <img id="showCodeImage" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>动态口令：</label>
                        <div class="wd250 m15_l">
                            <input type="text" name="code" class="form-control" maxlength="6" placeholder="请输入六位数动态口令" autocomplete="off"/>
                        </div>
                    </div>

                    <div class="form-group" id="divShowAttr" style="display:none;">
                        <label class="wd100 control-label"></label>
                        <div class="wd250 m15_l">
                            <label class="form-control noBorder">已扫描过二维码，可直接输入动态口令</label>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </section>
    <script type="text/javascript">
        var validator = null;
        var secret = null;
        var username = null;
        var isShowQR = 2;
        var isLogged = 2;
        var uId = 0;
        $(function (e) {
            username = FlashPay.Util.GetQueryString("username");
            secret = FlashPay.Util.GetQueryString("secret");
            isLogged = FlashPay.Util.GetQueryString("isLogged");
            isShowQR = FlashPay.Util.GetQueryString("isShowQR");
            uId = FlashPay.Util.GetQueryString("userId");
            if (parseInt(isLogged) == 1 && parseInt(isShowQR) == 2) {
                var userModel = {};
                userModel.Account = username;
                userModel.Password = secret;
                getCode(userModel);
            } else {
                $("#showCodeImage").attr("src", "");
                $("#showCodeDiv").hide();
                $("#divShowAttr").show();
            }
            validator = FlashPay.Util.Validator({
                formObj: $("#form"),
                rules: {
                    code: { required: true,digits: true }
                },
                messages: {
                    code: { required: "请输入六位数动态口令", digits:"动态口令必须为数字" }
                }
            });
        });

        //获取二维码
        function getCode(userModel) {
            var cover = FlashPay.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });
            FlashPay.Util.Ajax({
                type: "post",
                url: "/Account/GetCode",
                data: JSON.stringify(userModel),
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (!data.success) {
                        FlashPay.UI.Tip_short_warning(data.errorMessage);
                        return;
                    } else {
                        $("#showCodeDiv").show();
                        $("#showCodeImage").attr("src", data.successMessage);
                    }
                    cover.Remove();
                },
                error: function (json) {
                    cover.Remove();
                }
            });
        }

        function save() {
            if (!validator.form()) {
                return false;
            }

            var userModel = {};
            userModel.UserId = uId;
            userModel.USecretKey = secret;
            userModel.Code = $("input[name='code']").val();
            userModel.UShowQR = isShowQR;
            userModel.UserName = username;

            if ($("body").data("confirmReceive") == "lock") {
                return false;
            } else {
                $("body").data("confirmReceive", "lock");
            }

            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Account/CheckCode",
                data: JSON.stringify(userModel),
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    $("body").data("confirmReceive", "unlock");
                    if (!data.success) {
                        FlashPay.UI.Tip_short_warning(data.errorMessage);
                        $.removeCookie('username'); $.removeCookie('companyname'); $.removeCookie('token');$.removeCookie('popups')
                        return;
                    } else {
                        $.removeCookie('username');
                        $.removeCookie('companyname');
                        $.removeCookie('token');
                        FlashPay.UI.Tip_success("登录成功！");
                        var info = data.data;
                        $.cookie('username', info.userName);
                        $.cookie('companyname', info.companyName);
                        $.cookie('token', info.token);
                        $.removeCookie('popups')
                        window.parent.location.href = "/index.html";
                    }
                }
            });
        };
    </script>
</body>
</html>
